import 'package:enjoy_plus/pages/home/components/home_list.dart';
import 'package:enjoy_plus/pages/home/components/home_nav.dart';
import 'package:enjoy_plus/utils/http.dart';
import 'package:enjoy_plus/utils/toast.dart';
import 'package:flutter/material.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});
  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  void initState() {
    super.initState();
    // 获取公告列表数据
    getNotifyListData();
  }

  // 公告列表
  List notifyList = [];
  // 获取公告列表数据
  getNotifyListData() async {
       try {
      // 发送网络请求
      final res = await http.get('/announcement');
      // 成功轻提示
      ToastUtil.showInfo('数据加载成功');
      // 更新数据 + 视图
      setState(() {
        notifyList = res['data'];
      });
    } catch (error) {
      ToastUtil.showError('网络请求出现错误');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color.fromARGB(255, 229, 231, 241),
      appBar: AppBar(
        title: const Text('享+社区'),
        backgroundColor: Colors.transparent,
        centerTitle: true,
      ),
      body: ListView(
        padding: const EdgeInsets.all(10),
        children: [
          // 导航
          const HomeNav(),
          // 间距
          const SizedBox(height: 10),
          // 公告图
          ClipRRect(
            borderRadius: BorderRadius.circular(10),
            child: Image.asset('assets/images/banner@2x.jpg'),
          ),

          // 公告列表
          // 公告列表，传递数据给子组件
          HomeList(notifyList: notifyList),
        ],
      ),
    );
  }
}
